package com.tg.app.activity.device.add;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.appbase.custom.BuildConfig;
import com.appbase.custom.constant.CommonConstants;
import com.hjq.permissions.Permission;
import com.tg.app.R;
import com.base.BaseActivity;
import com.tg.app.report.DeviceAddReport;
import com.tg.app.report.DeviceAddReportBean;
import com.tg.app.util.LogUtils;
import com.tg.appcommon.android.PermissionUtil;
import com.tange.base.toolkit.PreferenceUtil;
import com.tg.appcommon.android.TGLog;


public class AddWifiChooseActivity extends BaseActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_add_wifi_choose);
        hideActionBar();
        initView();

        LogUtils.matTrackCustomKVEvent(this,"device_add","AD1");
        DeviceAddReportBean reportBean = new DeviceAddReportBean();
        reportBean.startTime = System.currentTimeMillis();
        reportBean.userID = String.valueOf(PreferenceUtil.getInt(this, CommonConstants.PRE_USER_ID));
        DeviceAddReport.getInstance().setReportBean(reportBean);
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected  void initView(){
        RelativeLayout add4g = findViewById(R.id.rel_add_4g);
        RelativeLayout wifiAdd = findViewById(R.id.rel_add_wifi);

        boolean hasWiFiCam = BuildConfig.device_wifiCamera_addByFast || BuildConfig.device_wifiCamera_addByQrCode || BuildConfig.device_wifiCamera_addByWired;
        boolean has4gCam =  BuildConfig.device_4gCamera_addByQrCode;

        // 只有WiFi的添加方式，那么直接去对应页面
        if (hasWiFiCam && !has4gCam) {
            launchAddWiFi();
            return;
        }

        // 只有WiFi的添加方式，那么直接去对应页面
        if (!hasWiFiCam && has4gCam) {
            launchAdd4G();
            return;
        }


        if (hasWiFiCam) {
            wifiAdd.setVisibility(View.VISIBLE);
        } else {
            wifiAdd.setVisibility(View.GONE);
        }

        if (has4gCam) {
            add4g.setVisibility(View.VISIBLE);
        } else {
            add4g.setVisibility(View.GONE);
        }

        Button back = findViewById(R.id.add_choose_btn_back);
        add4g.setOnClickListener(this);
        wifiAdd.setOnClickListener(this);
        back.setOnClickListener(this);

    }

    private void launchAddWiFi() {
        Intent toAp = new Intent(this, AddWifiDeviceActivity.class);
        startActivity(toAp);
        finish();
    }

    private final static String[] needQrcodeScanPermissions = {
            Permission.ACCESS_FINE_LOCATION,
            Permission.ACCESS_COARSE_LOCATION
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] paramArrayOfInt) {
        if (requestCode == AddFragmentTabActivity.PERMISSON_REQUESTCODE) {
            if (!PermissionUtil.verifyPermissions(paramArrayOfInt)) {      //没有授权
                TGLog.d("onRequestPermissionsResult = false");
            }else{
                TGLog.d("onRequestPermissionsResult = true");
            }
        }
    }

    private boolean requestPermission() {
        PermissionUtil permissionUtil = new PermissionUtil(AddWifiChooseActivity.this);
        return permissionUtil.checkPermissions(AddWifiChooseActivity.this, AddFragmentTabActivity.PERMISSON_REQUESTCODE, AddWifiChooseActivity.needQrcodeScanPermissions);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rel_add_4g) {
            launchAdd4G();
        } else if (id == R.id.rel_add_wifi) {
            launchAddWiFi();
        } else if (id == R.id.add_choose_btn_back) {
            this.finish();
        }
    }

    private void launchAdd4G() {
        DeviceAddReportBean bean = DeviceAddReport.getInstance().getReportBean();
        int val = AddFragmentTabActivity.DEVICE_ADD_TYPE_QRCODE_SCAN;
        if (bean != null) {
            bean.title = "4g_qrcode";
            DeviceAddReport.getInstance().setReportBean(bean);
        }
        Intent toQr = new Intent(this, AddFragmentTabActivity.class);
        toQr.putExtra(AddFragmentTabActivity.EXT_DEVICE_ADD_TYPE, val);
        startActivity(toQr);
        finish();
    }
}
